package com.ydd.service.impl;

import com.ydd.dto.SysConfigDTO;
import com.ydd.dto.SysConfigUpdateChildrenDTO;
import com.ydd.dto.SysConfigUpdateDTO;
import com.ydd.entity.SysConfig;
import com.ydd.mapper.SysConfigMapper;
import com.ydd.service.SysConfigService;
import com.ydd.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysConfigServiceImpl implements SysConfigService {

    /**
     * 获取索引配置
     *
     * @return
     */
    @Override
    public SystemConfigVO getIndexConfig() {
        // 定义配置类vo
        SystemConfigVO systemConfigVO = new SystemConfigVO();
        // 获取系统配置集合
        List<SysConfig> sysConfig = sysConfigMapper.all();
        // 遍历集合获取所需配置
        for (SysConfig config : sysConfig) {
            switch (config.getAttrKey()) {
                case "login_title":
                    systemConfigVO.setLoginTitle(config.getAttrValue());
                    break;
                case "login_bg":
                    systemConfigVO.setLoginBg(config.getAttrValue());
                    break;
                case "system_name":
                    systemConfigVO.setSystemName(config.getAttrValue());
                    break;
                case "system_logo":
                    systemConfigVO.setSystemLogo(config.getAttrValue());
                    break;
                case "web_ico":
                    systemConfigVO.setWebIco(config.getAttrValue());
                    break;
            }
        }

        return systemConfigVO;
    }


    @Autowired
    private SysConfigMapper sysConfigMapper;
    /**
     * 增加配置
     *
     * @param sysConfigDTO
     */
    @Override
    public void add(SysConfigDTO sysConfigDTO) {
        //new一个数据库的表 sysConfig
        SysConfig sysConfig = new SysConfig();
        //把sysConfigDTO加入到sysConfig数据库表中
        BeanUtils.copyProperties(sysConfigDTO,sysConfig);
        //执行SysConfigMapper的add方法添加数据库
        sysConfigMapper.add(sysConfig);
    }

    /**
     * 获取所有配置数据
     *
     * @return
     */
    @Override
    public SysConfigVO all() {
        SysConfigVO sysConfigVO = new SysConfigVO();
        //调用接口查询所有数据信息
        List<SysConfig> allData = sysConfigMapper.all();
        //封装ConfigVO
        sysConfigVO.setAllData(allData.toArray());
        //封装configForm
        ConfigFormVO configFormVO = ConfigFormVO.builder()
                .blackIp(sysConfigMapper.selectByattrKey("black_ip"))
                .closeWebsite(sysConfigMapper.selectByattrKey("close_website"))
                .copyCode(sysConfigMapper.selectByattrKey("copy_code"))
                .copyInfo(sysConfigMapper.selectByattrKey("copy_info"))
                .copyUrl(sysConfigMapper.selectByattrKey("copy_url"))
                .loginBg(sysConfigMapper.selectByattrKey("login_bg"))
                .loginTitle(sysConfigMapper.selectByattrKey("login_title"))
                .loginregBg(sysConfigMapper.selectByattrKey("loginreg_bg"))
                .noticContent(sysConfigMapper.selectByattrKey("notic_content"))
                .noticTitle(sysConfigMapper.selectByattrKey("notic_title"))
                .phone(sysConfigMapper.selectByattrKey("phone"))
                .siteKeywords(sysConfigMapper.selectByattrKey("site_keywords"))
                .siteLink(sysConfigMapper.selectByattrKey("site_link"))
                .siteName(sysConfigMapper.selectByattrKey("site_name"))
                .systemLogo(sysConfigMapper.selectByattrKey("system_logo"))
                .systemName(sysConfigMapper.selectByattrKey("system_name"))
                .webIco(sysConfigMapper.selectByattrKey("web_ico"))
                .webImg(sysConfigMapper.selectByattrKey("web_img"))
                .build();
        sysConfigVO.setConfigForm(configFormVO);
        //封装configRules
        ConfigRulesVO configRulesVO = ConfigRulesVO.builder()
                .siteName(sysConfigMapper.selectByattrKey("site_name"))
                .build();
        sysConfigVO.setConfigRules(configRulesVO);

        //封装list
        List<ListVO> listVOList =new ArrayList<>();
        List<SysConfig> sysConfigList = sysConfigMapper.all();
        //遍历循环，把第一个tab_value不同的拿出来，单独作为一个集合封装
        for (SysConfig sysConfig : sysConfigList) {
            //遍历listVo,查看是否有重复的key，没有则创新的对象,重写equal
            if(listVOList!=null && listVOList.size()>0){
                //count用来计数为0则为全不相同
                int count = listVOList.size();
                //必须所有list都没有才新建ListVO对象
                for (ListVO list : listVOList) {
                    if (!list.getKey().equals(sysConfig.getTabValue())){
                        count--;
                        continue;
                    }
                    if (list.getKey().equals(sysConfig.getTabValue())){
                        break;
                    }
                }
                if(count==0){
                    ListVO listVO = new ListVO();
                    //封装key，name
                    listVO.setKey(sysConfig.getTabValue());
                    listVO.setName(sysConfig.getGroupValue());
                    listVOList.add(listVO);
                }
            }else {
                ListVO listVO = new ListVO();
                //封装key，name
                listVO.setKey(sysConfig.getTabValue());
                listVO.setName(sysConfig.getGroupValue());
                listVOList.add(listVO);
            }
        }
        //遍历list
        for (ListVO listVO : listVOList) {
            //目标：封装children
            List<ChildrenVO> childrenVOList = new ArrayList<>();
            //遍历sysConfigList 找key相同,id不同的封装
            for (SysConfig sysConfig : sysConfigList) {

                if(sysConfig.getTabValue().equals(listVO.getKey())){
                    listVO.setName(sysConfig.getGroupValue());
                    //封装data
                    ChildrenVO childrenVO = new ChildrenVO();
                    List<Object> dataList = new ArrayList<>();
                    //遍历遍历sysConfigList，找group相同的装到dataList，最后datalist转data数组
                    for (SysConfig config : sysConfigList) {
                        if(sysConfig.getGroupValue().equals(config.getGroupValue())){
                            dataList.add(config);
                        }
                    }
                    childrenVO.setData(dataList.toArray());
                    childrenVO.setName(sysConfig.getGroupValue());
                    int countChild = childrenVOList.size();
                    for (ChildrenVO child : childrenVOList) {
                        if(child.getName().equals(childrenVO.getName())){
                            break;
                        }
                        if (!child.getName().equals(childrenVO.getName())){
                            countChild--;
                        }
                    }
                    if(countChild==0){
                        childrenVOList.add(childrenVO);
                    }
                }
            }
            listVO.setChildren(childrenVOList.toArray());
        }
        sysConfigVO.setList(listVOList.toArray());

        return sysConfigVO;
    }

    /**
     * 修改配置信息
     *
     * @param SysConfigUpdateChildrenDTOList
     */
    @Override
    public void update(List<SysConfigUpdateChildrenDTO> SysConfigUpdateChildrenDTOList) {
        Object[] sysConfigDTOArray = SysConfigUpdateChildrenDTOList.toArray();
        for (Object sysConfigDTO : sysConfigDTOArray) {
            SysConfig sysConfig = new SysConfig();
            BeanUtils.copyProperties(sysConfigDTO,sysConfig);
            sysConfigMapper.updateById(sysConfig);
        }
    }
}
